package com.example.yrdev.ordering_system.Adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Database.RefundGoodTB;
import com.example.yrdev.ordering_system.Model.CardRecord;
import com.example.yrdev.ordering_system.Model.PurchaseReport;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by luke on 12/19/16.
 */

public class GeneraLReportAdapter extends BaseExpandableListAdapter {

    private LayoutInflater layoutInflater;
    String[] titleArr;
    Context context;

    public GeneraLReportAdapter(Context context,String[] titleArr){
        layoutInflater=LayoutInflater.from(context);
        this.context=context;

        this.titleArr=titleArr;

    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.general_report_child_row1,null);

//            switch (groupPosition){
//
//                default:
//
//
//                    break;
//            }
        }

        TextView balance=(TextView)convertView.findViewById(R.id.balance);
        TextView count=(TextView)convertView.findViewById(R.id.count);


        configureDetail(groupPosition,count,balance);


        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public int getGroupCount() {
        return titleArr.length;

    }

    @Override
    public long getGroupId(int groupPosition) {

        return 0;

    }
    public Double totalIncome=0.;


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

      if (convertView==null){

         convertView=layoutInflater.inflate(R.layout.general_repot_group,null);

      }
        TextView groupTit=(TextView) convertView.findViewById(R.id.group_title);
        groupTit.setText(titleArr[groupPosition]);

        TextView balance=(TextView)convertView.findViewById(R.id.balance);

        if (groupPosition%2==0){

            balance.setTextColor(ContextCompat.getColor(context,R.color.green));

        }else {

            balance.setTextColor(ContextCompat.getColor(context,R.color.redColor));

        }

        configureDetail(groupPosition,null,balance);

        return convertView;

    }
    private void configureDetail(int position,TextView count,TextView balance){
        String balanceVal="";
        String countVal="";

       try {
        switch (position){
            case 0:
            {
                balanceVal=String.valueOf(getFixRandomDetail().get("amount"));
                countVal=String.valueOf(getFixRandomDetail().get("count"));

            }
            break;
            case 1:{
                balanceVal=String.valueOf(getCancelPurchaseDetail().get("amount"));
                countVal=String.valueOf(getCancelPurchaseDetail().get("count"));
            }
                break;
            case 2:
            {

                 balanceVal =String.valueOf(getGoodPurchaseDetail(0).get("amount"));
                countVal=String.valueOf(getGoodPurchaseDetail(0).get("count"));
            }
            break;

            case 3:
            {
                balanceVal=String.valueOf(getRefundGoodPurchaseDetail(0).get("amount"));
                countVal=String.valueOf(getRefundGoodPurchaseDetail(0).get("count"));
            }
                break;

            case 4:

                 balanceVal=String.valueOf(getGoodPurchaseDetail(3).get("amount"));
                countVal=String.valueOf(getGoodPurchaseDetail(3).get("count"));
                break;

            case 5:
            {
                balanceVal=String.valueOf(getRefundGoodPurchaseDetail(3).get("amount"));
                countVal=String.valueOf(getRefundGoodPurchaseDetail(3).get("count"));
            }
            break;


        }
           String val=balanceVal+"元";
           if (position%2==0){
               val="+ "+val;
               totalIncome=totalIncome+Double.valueOf(balanceVal);


           }else {
               val="- "+val;
               totalIncome=totalIncome-Double.valueOf(balanceVal);

           }
           balance.setText(val);

           if (count!=null){

            count.setText(countVal);

           }

       }catch (Exception e){

           GlobalFunction.printTextByTag(e.getMessage());
       }

    }

    private HashMap getFixRandomDetail(){

      ArrayList <CardRecord> cardRecordArrayList= CardRecord.getRecordListWithType(context,7);
        Double price=0.;
        for (CardRecord report:cardRecordArrayList){


            price=price+report.getPurchaseAmount();


        }
        HashMap<String,Object>hashMap=new HashMap<>();
        hashMap.put("count",cardRecordArrayList.size());
        hashMap.put("amount",price);
        return hashMap;

    }

    private HashMap getCancelPurchaseDetail(){

        ArrayList <CardRecord> cardRecordArrayList= CardRecord.getRecordListWithType(context,7);
        Double price=0.;
        for (CardRecord report:cardRecordArrayList){

            if (report.getRefundIndex()==1)//已退过
                price=price+report.getPurchaseAmount();


        }
        HashMap<String,Object>hashMap=new HashMap<>();
        hashMap.put("count",cardRecordArrayList.size());
        hashMap.put("amount",price);
        return hashMap;

    }


    private HashMap getGoodPurchaseDetail(int paymentType){//0:现金，3；刷卡

        ArrayList<PurchaseReport>purchaseReportArrayList= PurchaseReport.getPurchaseReportBaseOnType(context,paymentType);

        HashMap<String,Object>hashMap=new HashMap<>();
        Double price=0.;
        for (PurchaseReport report:purchaseReportArrayList){

            price=price+Double.valueOf(report.getSaleCost());

        }
        hashMap.put("count",purchaseReportArrayList.size());
        hashMap.put("amount",price);

        return hashMap;

    }

    private HashMap getRefundGoodPurchaseDetail(int paymentType){//0:现金，3；刷卡

        ArrayList<RefundGoodTB.Item>purchaseReportArrayList= RefundGoodTB.getAllRefundItemBaseOnType(context,paymentType);

        HashMap<String,Object>hashMap=new HashMap<>();
        Double price=0.;
        for (RefundGoodTB.Item report:purchaseReportArrayList){

            price=price+report.getTotalPrice();



        }
        hashMap.put("count",purchaseReportArrayList.size());
        hashMap.put("amount",price);

        return hashMap;

    }
}
